<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type='text/javascript'>
        //<![CDATA[

        // @todo 获取所选文本的开始和结束位置
        function getPositions() {
            var el = document.getElementById('selected');
            var startPosition = 0;//所选文本的开始位置
            var endPosition = 0;//所选文本的结束位置
            if(document.selection) {
//IE
                var range = document.selection.createRange();//创建范围对象
                var drange = range.duplicate();//克隆对象

                drange.moveToElementText(el);  //复制范围
                drange.setEndPoint('EndToEnd', range);

                startPosition = drange.text.length - range.text.length;
                endPosition = startPosition + range.text.length;
            }
            else if(window.getSelection) {
//Firefox,Chrome,Safari etc
                startPosition = el.selectionStart;
                endPosition = el.selectionEnd;
            }
            return {
                'start':startPosition,
                'end':endPosition
            }
        }

        //@todo 获取textarea中，选中的文本
        function getSelected() {
            var position = getPositions();
            var start = position.start;//开始位置
            var end = position.end;//结束位置
            var text = document.getElementById('selected').value;
            var selectText = text.substr(start, (end - start));//textarea中，选中的文本
            console.log(selectText);
        }
        //]]>
    </script>
</head>
<body>
<textarea id='selected'>
获取textarea中选中文本的开始与结束位置!
</textarea> <button onclick='getSelected()'>获取内容</button>
</body>
</html>